package com.hyc.myvideo.ui.activity.home;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.PurchaseDetails;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.utils.StringUtils;

/**
 * 确认购买
 * Created by dfeads on 2017/8/11.
 */

public class ConfirmPurchaseActivity extends BaseFActivity {
  /** 课程 */
  @BindView(R.id.activity_confirm_purchase_tv_course) TextView mCourse;
  /** 当前身份 */
  @BindView(R.id.activity_confirm_purchase_tv_identity) TextView mIdentity;
  /** 当前余额 */
  @BindView(R.id.activity_confirm_purchase_tv_balance) TextView mBalance;
  /** 课程费用(非VIP) */
  @BindView(R.id.activity_confirm_purchase_tv_cost) TextView mCost;
  /** 课程费用(VIP) */
  @BindView(R.id.activity_confirm_purchase_tv_cost_vip) TextView mCostVip;
  /** 每次只能使用一张优惠卷 模块 */
  @BindView(R.id.activity_confirm_purchase_tv_my_coupon) TextView mMyCoupon;
  /** 优惠卷总数量 */
  @BindView(R.id.activity_confirm_purchase_tv_my_coupon_num) TextView mMyCouponNum;
  /** 优惠卷抵用的露珠数  左边 */
  @BindView(R.id.activity_confirm_purchase_coupon_money) TextView mMoney;
  /** 优惠卷取消按钮 */
  @BindView(R.id.activity_confirm_purchase_coupon_cancel) ImageView mCouponCancel;
  /** 优惠卷名称 */
  @BindView(R.id.activity_confirm_purchase_coupon_tv_title) TextView mCouponTitle;
  /** 优惠卷抵用的露珠数  下边 */
  @BindView(R.id.activity_confirm_purchase_coupon_tv_money) TextView mCouponMoney;
  /** 优惠卷时间 */
  @BindView(R.id.activity_confirm_purchase_coupon_tv_time) TextView mCouponTime;
  /** 优惠卷模块 */
  @BindView(R.id.activity_confirm_purchase_rl_coupon) RelativeLayout mCoupon;
  /** 实际支付费用 */
  @BindView(R.id.activity_confirm_purchase_tv_payment) TextView mPayment;
  /** 确认 */
  @BindView(R.id.activity_confirm_purchase_bt_confirm) Button mConfirm;
  /** 参数id（课程id/直播课程id） */
  private String parame_id = "";
  /** 3=课程购买,4=直播购买 */
  private int pay_state = 3;

  private PurchaseDetails purchaseDetails;

  @Override public int getContentViewId() {
    return R.layout.activity_confirm_purchase;
  }

  @Override protected void initView() {
    setLeftBtnClick(true, "购买确认");
  }

  @Override protected void initData() {
    parame_id = getIntent().getStringExtra("id");
    pay_state = getIntent().getIntExtra("type", 3);
    if (!StringUtils.isEmpty(parame_id)) {
      api.getPayDetails(userInfo.getUser_id(), parame_id, pay_state);
    }
  }

  @Override protected void oNext(String resulte, String method) {
    BaseResult<PurchaseDetails> parseObject =
        JSONObject.parseObject(resulte, new TypeReference<BaseResult<PurchaseDetails>>() {
        });
    if (parseObject.isCode()) {
      purchaseDetails = parseObject.getResult();
      setView();
    } else {
      Toaster.show(parseObject.getMsg());
    }
  }

  private void setView() {
    if (purchaseDetails == null) {
      return;
    }
    mCourse.setText(purchaseDetails.getName());
    mIdentity.setText(purchaseDetails.getVip_name());
    mBalance.setText(purchaseDetails.getUser_balance() + "颗");
    mCost.setText(purchaseDetails.getCharge_luzhu() + "颗");
    mCostVip.setText(purchaseDetails.getVip_luzhu() + "颗");
    mMyCoupon.setVisibility(View.VISIBLE);
    mCoupon.setVisibility(View.INVISIBLE);
    mMyCouponNum.setText(purchaseDetails.getGiev_count() + "张");
    //mPayment.setText();
  }

  @Override protected void oError() {

  }

  @OnClick({
      R.id.activity_confirm_purchase_rl_my_coupon, R.id.activity_confirm_purchase_coupon_cancel,
      R.id.activity_confirm_purchase_bt_confirm
  }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.activity_confirm_purchase_rl_my_coupon://我的优惠卷
        break;
      case R.id.activity_confirm_purchase_coupon_cancel://取消优惠卷
        break;
      case R.id.activity_confirm_purchase_bt_confirm://确定
        break;
    }
  }
}
